package com.jiunuan.live.web.starter.context;

import com.jiunuan.live.common.interfaces.enums.GatewayHeaderEnum;
import com.jiunuan.live.web.starter.constants.RequestConstants;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class JiuNuanUserInfoInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userIdStr = request.getHeader(GatewayHeaderEnum.USER_LOGIN_ID.getName());
        // 参数判断。userId是否为空
        //可能走的是白名单url
        if(StringUtils.isEmpty(userIdStr)){
            System.out.println("白名单请求：放行");
            return true;
        }
        // 如果非空，则把他放在线程本地变量中
        JiuNuanRequestContext.set(RequestConstants.JiuNuan_USER_ID, Long.valueOf(userIdStr));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        JiuNuanRequestContext.clear();
    }
}
